package com.umfwechat.util;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;


import com.umpay.sp.exception.BaseException;
import com.umpay.sp.model.common.ReqMessage;
import com.umpay.sp.model.common.RetCode;
import com.umpay.sp.util.StringUtil;
import com.umpay.sp.xml.XmlData;
import com.umpay.util.HttpUtil;



public class SmsUtil {
	private static final Logger log = LoggerManager.getBusiLogger();
	/**
	 * 调用商户自接入短信验证码
	 * @author 
	 * @param req
	 * @param serviceRest2
	 * @return
	 * @throws Exception
	 */
	public static Map<String,Object> requestSpapply(ReqMessage req, String serviceRest2) throws Exception{
		String url =serviceRest2;
		Map<String,Object> params = new HashMap<String,Object>();		
		//移除reqMap中空的内容 否则会报空指针
		Map<String, String> reqMap = req.getReqMap();
		List<String> rmKeys = new ArrayList<String>();
		for(Entry<String, String> e : reqMap.entrySet()){
			if(null == e.getValue()){
				rmKeys.add(e.getKey());
			}
		}
		for(String key: rmKeys){
			reqMap.remove(key);
		}	
		params.putAll(reqMap);
		params.put(XmlData.RPID, req.getRpid());
		params.put(XmlData.REQDATE, req.getReqDate());
		params.put(XmlData.REQTIME, req.getReqTime());
		log.info(apLog(req.getRpid(),"httpPostForm2Xml请求数据：", req, ",url=", url));	
		Map<String,Object> result = HttpUtil.httpPostForm2Xml(url, params);
		log.info(apLog(req.getRpid(),"httpPostForm2Xml返回结果：",result));
		return result;
	}
	
	
	/**
	 * 字符串拼接记录日志
	 * @param rpid
	 * @param parts
	 * @return
	 */
	public static String apLog(String rpid, Object... parts){
		return ap("rpid[",rpid,"]") + ap(parts);
	}
	/**
	 * 字符串拼接
	 * @param parts
	 * @return
	 */
	public static String ap(Object... parts){
		StringBuilder sb = new StringBuilder();
		for(Object p: parts){
			sb.append(p);
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		String regex = "/1[0-9]{10}/";
		System.out.println(regex.matches("18210528852"));
	}
	/**
	 * <br>description : 下发短信
	 * @throws 		Exception
	 * @author      
	 * @version     1.0
	 * @date        2015年9月18日 14:33:37
	 * @param       ReqMessage req 包含
	 * 				String mobileNo,   * 必传
	 * 				String merId,      * 必传
	 * 				String origAmt,    * 必传
	 * 				String rpid,       * 必传
	 * 				String reqDate,    * 必传
	 * 				String reqTime     * 必传
	 */
	public static Map<String,Object>  sendMsg(ReqMessage req, String url) throws Exception{
			log.info("下发短信请求数据：" + req);
			Map res = requestSpapply(req, url);
			log.info("下发短信返回结果：" + res);
			if(!RetCode.SUCCESS.equals(StringUtil.trim(res.get(XmlData.RETCODE)))){
				String retMsg = (String) res.get(XmlData.RETMSG);
				log.error("短信验证码发送失败，请重新获取!");
				throw new BaseException("短信验证码发送失败，请重新获取");
			}
			return res;
	}
	public static Map<String,Object> testMsg(ReqMessage req, String url) throws Exception{
		log.info("验证验证码请求数据：" + req);
		//String serviceRest2 = StringUtil.trim(PropertyUtil.getStrValue(SPAPPLYPLATWEB_CONFIG_PROPERTIES,"spaply.rest2.service.spallyMessage",""));
		Map res = requestSpapply(req, url);
		log.info("验证验证码返回结果：" + res);
		return res;
	}
	
}
